package com.feel.mall.userservice.service;

import com.feel.mall.common.bean.Addr;
import com.feel.mall.common.exception.FeelException;
import com.feel.mall.common.service.AddrService;
import com.feel.mall.userservice.mapper.AddrMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * @Author: 刘永钦
 * @Description:
 */
@DubboService
public class AddrServiceImpl implements AddrService {
    @Autowired
    private AddrMapper addrMapper;

    @Override
    public Boolean insert(Addr addr) {
        Addr condition = new Addr();
        condition.setUserId(addr.getUserId());
        List<Addr> list = addrMapper.selectByCondition(condition);
        if (list != null && !list.isEmpty()) {
            addr.setStatus(0);
        } else {
            addr.setStatus(1);
        }
        return addrMapper.insert(addr) == 1;
    }

    @Override
    public Boolean delete(Integer id, Integer userId) throws FeelException {
        // 判断地址是否存在
        Addr addr = addrMapper.selectById(id);
        if (addr == null) {
            throw new FeelException("地址不存在");
        }
        if (!addr.getUserId().equals(userId)) {
            throw new FeelException("权限错误");
        }
        Integer i = addrMapper.delete(id);
        // 如果删除的是默认地址，将其他地址设置为默认地址
        if (i > 0 && addr.getStatus().equals(1)) {
            Addr condition = new Addr();
            condition.setUserId(userId);
            List<Addr> list = addrMapper.selectByCondition(condition);
            if (list != null && !list.isEmpty()) {
                list.get(0).setStatus(1);
                addrMapper.update(list.get(0));
            }
        }
        return i == 1;
    }

    @Override
    public Boolean update(Addr addr, Integer userId) throws FeelException {
        // 判断地址是否存在
        Addr localAddr = addrMapper.selectById(addr.getId());
        if (localAddr == null) {
            throw new FeelException("地址不存在");
        }
        if (!localAddr.getUserId().equals(userId)) {
            throw new FeelException("权限错误");
        }
        addr.setStatus(null);
        return addrMapper.update(addr) == 1;
    }

    @Override
    public List<Addr> getByCondition(Addr condition) {
        return addrMapper.selectByCondition(condition);
    }

    @Override
    public Addr getById(Integer id, Integer userId) throws FeelException {
        // 判断地址是否存在
        Addr localAddr = addrMapper.selectById(id);
        if (localAddr == null) {
            throw new FeelException("地址不存在");
        }
        if (!localAddr.getUserId().equals(userId)) {
            throw new FeelException("权限错误");
        }

        return addrMapper.selectById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void setDefaultAddr(Integer id, Integer userId) throws FeelException {
        Addr condition = new Addr();
        condition.setUserId(userId);
        // 获取用户地址
        List<Addr> addrList = addrMapper.selectByCondition(condition);
        //  判断是否存在地址
        if (addrList == null || addrList.isEmpty()) {
            throw new FeelException("错误，请重试");
        }
        // 设置标识
        AtomicBoolean b = new AtomicBoolean(false);
        // 对地址进行判断
        addrList.forEach(addr -> {
            if (addr.getStatus().equals(1)) {
                addr.setStatus(0);
            }
            if (addr.getId().equals(id)) {
                addr.setStatus(1);
                b.set(true);
            }
        });
        // 对标识进行判断
        if (!b.get()) {
            throw new FeelException("地址不存在");
        }

        addrList.forEach(addr -> {
            addrMapper.update(addr);
        });
    }
}
